package com.jule.robot.strategy.impl;

import JoloProtobuf.GameSvr.JoloGame;
import com.jule.robot.model.CardValueModel;
import com.jule.robot.model.HandCardTypeModel;
import com.jule.robot.model.PlayerInfo;
import com.jule.robot.service.holder.CardValueHolder;
import com.jule.robot.service.holder.FunctionIdHolder;
import com.jule.robot.service.websocket.BaseWebSocketClient;
import com.jule.robot.strategy.BaseRobotStrategry;
import com.jule.robot.valve.gate.RobotConfigHandler;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class SeeCardStrategry extends BaseRobotStrategry {
    private final static Logger logger = LoggerFactory.getLogger(SeeCardStrategry.class);

    public SeeCardStrategry() {
    }

    @Override
    public void doAction(BaseWebSocketClient client, int functionId, byte[] bytes) {
//        setClient(client);
        JoloGame.JoloGame_ApplySeeCardAck ack = null;
        try {
            ack = JoloGame.JoloGame_ApplySeeCardAck.parseFrom(bytes);
            CardValueModel mixedCardModel = CardValueHolder.getCardValueModel(client.getMixedCardIndex());
            if (logger.isDebugEnabled()) {
                logger.debug("收到 看牌 结果。AckSeeCard Header, functionId->" + functionId + ", functionName->" + FunctionIdHolder.GetFunctionName(functionId) + ", ACK Body, result->" + ack.getResult() + ", ResultMsg->" + ack.getResultMsg()
                        + ", userId->" + ack.getUserId() + ", roomId->" + ack.getRoomId() + ", tableId->" + ack.getTableId() + ", gameOrderId->" + ack.getGameOrderId() + ", cardSize->" + ack.getHandCardsCount()
                        + ", handCard->" + getHandCardString(ack.getHandCardsList()) + ", mixedCard->" + (null == mixedCardModel ? "null" : mixedCardModel.toString()) + ", mixCardIndex->" + client.getMixedCardIndex());
            }
            if (ack.getResult() == 1 && ack.getHandCardsCount() >= 2) {
                PlayerInfo playerInfo = client.getOnTablePlayers().get(ack.getUserId());
                if (playerInfo != null) {
                    client.getOnTablePlayers().get(ack.getUserId()).setIsBlind(0);
                }

                List<Integer> listCards = new ArrayList();
                for (Integer cardId : ack.getHandCardsList()) {
                    int cardCompareValue = CardValueHolder.getCardValueModel(cardId).getCompareValue();
                    if (null == mixedCardModel || cardCompareValue != mixedCardModel.getCompareValue()) { //如果不是混牌，那么将手牌放到手牌列表中
                        listCards.add(cardId);
                    } else {
                        logger.debug("机器人手牌中有混牌，需要从手牌列表中去掉混牌。 userId->{}, roomId->{}, tableId->{}, handCardId->{}, mixedCard->{}, gameOrderId->{}",
                                ack.getUserId(), ack.getRoomId(), ack.getTableId(), CardValueHolder.getCardValueString(cardId), mixedCardModel.toString(), ack.getGameOrderId());
                    }
                }
                playerInfo = new PlayerInfo();
                playerInfo.setHandCards(listCards.stream().mapToInt(i -> i).toArray());
                HandCardTypeModel cardModel = playerInfo.getHandCardTypeModel(client.getGameId());
                client.setCardModel(cardModel);
//                String cardLevel = RobotConfigHandler.getCardLevel(client.getGameId()+"", cardModel.getTypeCompareValue());
                logger.debug("替换混牌后的手牌结果。 gameId->" + client.getGameId() + ", userId->" + ack.getUserId() + ", handCard->" + cardModel.toStringCard() + ", cardType->" + cardModel.getTypeCompareValue());
            }
        } catch (Exception ex) {
            logger.error("AckSeeCard error, msg = " + ex.getMessage(), ex);
        }
    }

    private String getHandCardString(List<Integer> cardList) {
        StringBuilder sb = new StringBuilder();
        for (Integer cardId : cardList) {
            sb.append(CardValueHolder.getCardValueString(cardId) + " ");
        }
        return sb.toString();
    }
}
